home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / Inventory.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  44 lines

  1. //: C21:Inventory.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. #ifndef INVENTORY_H
  7. #define INVENTORY_H
  8. #include <iostream>
  9. #include <cstdlib>
  10. #include <ctime>
  11.  
  12. class Inventory {
  13.   char item;
  14.   int quantity;
  15.   int value;
  16. public:
  17.   Inventory(char it, int quant, int val) 
  18.     : item(it), quantity(quant), value(val) {}
  19.   // Synthesized operator= & copy-constructor OK
  20.   char getItem() const { return item; }
  21.   int getQuantity() const { return quantity; }
  22.   void setQuantity(int q) { quantity = q; }
  23.   int getValue() const { return value; }
  24.   void setValue(int val) { value = val; }
  25.   friend std::ostream& operator<<(
  26.     std::ostream& os, const Inventory& inv) {
  27.     return os << inv.item << ": " 
  28.       << "quantity " << inv.quantity 
  29.       << ", value " << inv.value;
  30.   }
  31. };
  32.  
  33. // A generator:
  34. struct InvenGen {
  35.   InvenGen() { std::srand(std::time(0)); }
  36.   Inventory operator()() {
  37.     static char c = 'a';
  38.     int q = std::rand() % 100;
  39.     int v = std::rand() % 500;
  40.     return Inventory(c++, q, v);
  41.   }
  42. };
  43. #endif // INVENTORY_H ///:~
  44.